<?php
// module for generate VRML code from ontology (array)

Class relationships{
    var $id,
        $x1,
        $x2,
        $y1,
        $y2,
        $z1,
        $z2;
};

function write_class($s, &$output){
    $output .= $s;
};

function in_array_class($s, $array_class, &$array_index1, &$array_index2){
    $b = false;
    $ind1 = 0;$ind2 = 0;
    $array_index1 = Array();
    $array_index2 = Array();
    for ($i = 0; $i < count($array_class); $i++){
        if ($s == $array_class[$i]->name1){
            $b = true;
            $array_index1[$ind1] = $i;
            $ind1++;
        };
        if ($s == $array_class[$i]->name2){
            $b = true;
            $array_index2[$ind2] = $i;
            $ind2++;
        };
    };
    return $b;
};

function child_class($class_id, $parent_class_id, $parent_class_position, $list_of_iattrs_id, $list_of_iattrs_name, &$acon, &$compatible, &$hierarchy, $x_c, $z_c, $y, $R, $deltaR, $deltay, $lod, &$output, &$viewpoints){
    global $_GET;
    $sql = "SELECT c.id, t.text, 0 as class_attr FROM class c, term t WHERE c.term_id = t.id and t.lang_id = 0 and c.parent_id = ".$class_id;
    $sql .= " UNION ";
    $sql .= "SELECT a.id, t.text, 1 as class_attr FROM attr a, class_attr ca, term t WHERE a.term_id = t.id and t.lang_id = 0 and a.id = ca.attr_id and ca.class_id = ".$class_id;
    $i_max = getMultData($sql, $list_of_elements);
    
    $temp_attr_list_id = Array();
    $temp_attr_list_name = Array();
    for ($i = 0; $i < $i_max; $i++){
      $list_of_elements[$i]["text"] = htmlspecialchars($list_of_elements[$i]["text"], ENT_QUOTES);
      if ($list_of_elements[$i]["class_attr"] == 1){
        $index = count($temp_attr_list_id);
        $temp_attr_list_id[$index] = $list_of_elements[$i]["id"];
        $temp_attr_list_name[$index] = $list_of_elements[$i]["text"];
      };
    };
    for ($i = 0; $i < count($list_of_iattrs_id); $i++){
      $list_of_elements[$i_max + $i]["id"] = $list_of_iattrs_id[$i];
      $list_of_elements[$i_max + $i]["text"] = $list_of_iattrs_name[$i];
      $list_of_elements[$i_max + $i]["class_attr"] = 1;
    };

    for ($i = 0; $i < count($temp_attr_list_id); $i++){
      $index = count($list_of_iattrs_id);
      $list_of_iattrs_id[$index] = $temp_attr_list_id[$i];
      $list_of_iattrs_name[$index] = $temp_attr_list_name[$i];
    };
    $i_max = count($list_of_elements);

    if ($i_max > 0){
      $alfa = (2*3.1415)/($i_max);//Coner
    };
    for ($i = 0; $i < $i_max; $i++){
        $z = $R * sin($i * $alfa);
        $x = $R * cos($i * $alfa);
        write_class(onto_LOD($x_c, $y, $z_c, $lod), $output);
        if ($i_max == 1){

          // if only one element for representation on this level
            $lines = $parent_class_position.','.$x_c.' '.$y.' '.$z_c;
            $next_parent_class_position = $x_c.' '.$y.' '.$z_c;
            if ($list_of_elements[$i]["class_attr"] == 0)
                write_class(onto_Class($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x_c, $y, $z_c), $output);
            else{
                if (!in_array($list_of_elements[$i]["id"], $temp_attr_list_id))
                  write_class(onto_Attributes($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x_c, $y, $z_c, 0.1, 0.5, 0.5), $output);
                else
                  write_class(onto_Attributes($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x_c, $y, $z_c, 0.1, 0.5, 0.9), $output);
            }
        }else{
          // if more than 1 element
          $x = $x + $x_c;
          $z = $z + $z_c;
          $lines = $parent_class_position.','.$x.' '.$y.' '.$z;
          $next_parent_class_position = $x.' '.$y.' '.$z;
          if ($list_of_elements[$i]["class_attr"] == 0)
              write_class(onto_Class($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x, $y, $z), $output);
          else{
              if (!in_array($list_of_elements[$i]["id"], $temp_attr_list_id))
                write_class(onto_Attributes($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x, $y, $z, 0.1, 0.5, 0.5), $output);
              else
                write_class(onto_Attributes($list_of_elements[$i]["text"], $list_of_elements[$i]["id"], $x, $y, $z, 0.1, 0.5, 0.9), $output);
          };
        };
        if ((isset($_GET["taxonomy"])) && ($_GET["taxonomy"] == 1))
            write_class(onto_relationship($lines, $parent_class_id.$list_of_elements[$i]["id"], "1 1 1"), $output);


        // Associative restriction processing
        if ((isset($_GET["acon"])) && ($_GET["acon"]==1)){
          $sql = "SELECT id, class2_id FROM acon WHERE class1_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $a_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($acon);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($a_rel_for_class[$j]["id"] == $acon[$k]->id){
                $acon[$k]->x1 = $x + 0.4;
                $acon[$k]->y1 = $y + 0.4;
                $acon[$k]->z1 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $acon[$index] = new relationships;
              $acon[$index]->id = $a_rel_for_class[$j]["id"];
              $acon[$index]->x1 = $x + 0.4;
              $acon[$index]->y1 = $y + 0.4;
              $acon[$index]->z1 = $z + 0.4;
            };
          };

          $sql = "SELECT id, class1_id FROM acon WHERE class2_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $a_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($acon);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($a_rel_for_class[$j]["id"] == $acon[$k]->id){
                $acon[$k]->x2 = $x + 0.4;
                $acon[$k]->y2 = $y + 0.4;
                $acon[$k]->z2 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $acon[$index] = new relationships;
              $acon[$index]->id = $a_rel_for_class[$j]["id"];
              $acon[$index]->x2 = $x + 0.4;
              $acon[$index]->y2 = $y + 0.4;
              $acon[$index]->z2 = $z + 0.4;
            };
          };

        };

        // Compatibility relationships processing
        if ((isset($_GET["compability"]))&&($_GET["compability"]==1)){
          $sql = "SELECT id, class2_id FROM compatible WHERE class1_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $c_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($compatible);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($c_rel_for_class[$j]["id"] == $compatible[$k]->id){
                $compatible[$k]->x1 = $x + 0.4;
                $compatible[$k]->y1 = $y + 0.4;
                $compatible[$k]->z1 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $compatible[$index] = new relationships;
              $compatible[$index]->id = $c_rel_for_class[$j]["id"];
              $compatible[$index]->x1 = $x + 0.4;
              $compatible[$index]->y1 = $y + 0.4;
              $compatible[$index]->z1 = $z + 0.4;
            };
          };

          $sql = "SELECT id, class1_id FROM compatible WHERE class2_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $c_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($compatible);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($c_rel_for_class[$j]["id"] == $compatible[$k]->id){
                $compatible[$k]->x2 = $x + 0.4;
                $compatible[$k]->y2 = $y + 0.4;
                $compatible[$k]->z2 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $compatible[$index] = new relationships;
              $compatible[$index]->id = $c_rel_for_class[$j]["id"];
              $compatible[$index]->x2 = $x + 0.4;
              $compatible[$index]->y2 = $y + 0.4;
              $compatible[$index]->z2 = $z + 0.4;
            };
          };

        };
        
        // Hierarchical relationsips prcessing
        if ((isset($_GET["hierarchy"]))&&($_GET["hierarchy"]==1)){
          $sql = "SELECT id, child_class_id FROM hierarchy WHERE parent_class_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $h_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($hierarchy);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($h_rel_for_class[$j]["id"] == $hierarchy[$k]->id){
                $hierarchy[$k]->x1 = $x + 0.4;
                $hierarchy[$k]->y1 = $y + 0.4;
                $hierarchy[$k]->z1 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $hierarchy[$index] = new relationships;
              $hierarchy[$index]->id = $h_rel_for_class[$j]["id"];
              $hierarchy[$index]->x1 = $x + 0.4;
              $hierarchy[$index]->y1 = $y + 0.4;
              $hierarchy[$index]->z1 = $z + 0.4;
            };
          };

          $sql = "SELECT id, parent_class_id FROM hierarchy WHERE child_class_id = ".$list_of_elements[$i]["id"];
          $j_max = getMultData($sql, $h_rel_for_class);
          for ($j = 0; $j < $j_max; $j++){
            $index = count($hierarchy);
            $flag = 0;
            for ($k = 0; $k < $index; $k++){
              if ($h_rel_for_class[$j]["id"] == $hierarchy[$k]->id){
                $hierarchy[$k]->x2 = $x + 0.4;
                $hierarchy[$k]->y2 = $y + 0.4;
                $hierarchy[$k]->z2 = $z + 0.4;
                $flag = 1;
              };
            };
            if ($flag == 0){
              $hierarchy[$index] = new relationships;
              $hierarchy[$index]->id = $h_rel_for_class[$j]["id"];
              $hierarchy[$index]->x2 = $x + 0.4;
              $hierarchy[$index]->y2 = $y + 0.4;
              $hierarchy[$index]->z2 = $z + 0.4;
            };
          };

        };
        
        //Represet class viewpoints
        if ($list_of_elements[$i]["class_attr"] == 0){
            $count_viewpoints = count($viewpoints);
            $viewpoints[$count_viewpoints] = new viewpoint_class;
            $viewpoints[$count_viewpoints]->id = $list_of_elements[$i]["id"];
            $viewpoints[$count_viewpoints]->name = $list_of_elements[$i]["text"];
            $viewpoints[$count_viewpoints]->x = $x;
            $viewpoints[$count_viewpoints]->y = $y;
            $viewpoints[$count_viewpoints]->z = $z;
        };
        write_class(onto_endLOD(), $output);

        if ($list_of_elements[$i]["class_attr"] == 0){
            
          $R = $R / $deltaR;
          //$deltaR = $deltaR - 2.5;
          $y = $y - $deltay; //distance between parent and child class
            
          child_class($list_of_elements[$i]["id"], $parent_class_id, $next_parent_class_position, $list_of_iattrs_id, $list_of_iattrs_name, &$acon, &$compatible, &$hierarchy, $x, $z, $y, $R, $deltaR, $deltay, $lod, &$output, &$viewpoints);
            
          $y = $y + $deltay; //interval between parent and child class
          $R = $R * $deltaR;
        };
    };
};
?>